package com.yhy.wxzn.wcs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yhy.wxzn.common.enums.WcsTaskTypeEnums;
import com.yhy.wxzn.common.exception.WmsBusinessException;
import com.yhy.wxzn.wcs.entity.WcsElevatorTask;
import com.yhy.wxzn.wcs.entity.WcsPlcProperty;
import com.yhy.wxzn.wcs.enums.TaskStatus;
import com.yhy.wxzn.wcs.mapper.WcsElevatorTaskMapper;
import com.yhy.wxzn.wcs.service.IWcsElevatorTaskService;
import com.yhy.wxzn.wcs.service.IWcsPlcPropertyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @Description: 提升机任务
 * @Author: jeecg-boot
 * @Date:   2023-11-08
 * @Version: V1.0
 */
@Service
public class WcsElevatorTaskServiceImpl extends ServiceImpl<WcsElevatorTaskMapper, WcsElevatorTask> implements IWcsElevatorTaskService {

    @Autowired
    private IWcsPlcPropertyService wcsPlcPropertyService;

    @Override
    public List<WcsElevatorTask> getUnfinishedByHoistList() {
        LambdaQueryWrapper<WcsElevatorTask> wrapper = new LambdaQueryWrapper<>();
        wrapper.lt(WcsElevatorTask::getTaskStatus, TaskStatus.FINISH.getTaskProgress())
                .in(WcsElevatorTask::getTaskType, Arrays.asList( WcsTaskTypeEnums.INBOUND_H.getValue(),
                        WcsTaskTypeEnums.OUTBOUND_H.getValue(),
                        WcsTaskTypeEnums.INBOUND_HC.getValue(),
                        WcsTaskTypeEnums.OUTBOUND_HC.getValue()))
                .orderByAsc(WcsElevatorTask::getCreateTime);
        return this.list(wrapper);
    }

    @Override
    public WcsPlcProperty findPlcPropertyByCoord(String source) {
        LambdaQueryWrapper<WcsPlcProperty> pQuery = new LambdaQueryWrapper<>();
        pQuery.eq(WcsPlcProperty::getCoord,source);
        List<WcsPlcProperty> propertyList = wcsPlcPropertyService.list(pQuery);
        if(CollectionUtils.isEmpty(propertyList)){
            throw new WmsBusinessException("根据点位:"+source+"未找到PLC配置的点位信息!");
        }
        return propertyList.get(0);
    }
}
